<!--
 * @FilePath     : /study_code/vue2.x/transition-reuse.html
 * @Description  : 过渡的复用
 * @Date         : 2025-05-03 08:44:46
 * @Author       : Fang YingGui
 * @LastEditors  : fangyinggui 18715384437@163.com
 * @LastEditTime : 2025-05-03 08:46:06
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="" />
    <style>
      /* 下面的样式 单文件应该放到 scoped 中 */
      .very-special-transition-enter-active,
      .very-special-transition-leave-active {
        transition: opacity 0.5s;
      }

      .very-special-transition-enter,
    .very-special-transition-leave-to

    /* .very-special-transition-leave-active below version 2.1.8 */ {
        opacity: 0;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <button v-on:click="changeCurrentComponentHandle">切换组件</button>
      <my-special-transition>
        <component :is="currentComponent"></component>
      </my-special-transition>
    </div>

    <script src="/vue/dist/vue.min.js"></script>
    <script src="./common.js"></script>
    <script>
 Vue.component('my-special-transition', {
        template:
          '\
        <transition\
          name="very-special-transition"\
          mode="out-in"\
          v-on:before-enter="beforeEnter"\
          v-on:after-enter="afterEnter"\
        >\
          <slot></slot>\
        </transition>\
      ',
        methods: {
          beforeEnter: function (el) {
            // ...
          },
          afterEnter: function (el) {
            // ...
          },
        },
      })
      // 函数式组件更适合完成这个任务：
      /*     Vue.component('my-special-transition', {
          functional: true,
          render: function (createElement, context) {
            var data = {
              props: {
                name: 'very-special-transition',
                mode: 'out-in'
              },
              on: {
                beforeEnter: function (el) {
                  // ...
                },
                afterEnter: function (el) {
                  // ...
                }
              }
            }
            return createElement('transition', data, context.children)
          }
        }) */
      // 自定义组件
      const myComponent1 = {
        data: function () {
          return {}
        },
        props: [],
        template: `<h2>transition复用1</h2>`,
        methods: {},
      }
      const myComponent2 = {
        data: function () {
          return {}
        },
        props: [],
        template: `<h2>transition复用2</h2>`,
        methods: {},
      }

      var app = new Vue({
        el: '#app',
        data: function () {
          return {
            currentComponent: 'myComponent1',
          }
        },
        components: {
          myComponent1,
          myComponent2,
        },
        methods: {
          changeCurrentComponentHandle() {
            this.currentComponent === 'myComponent2'
              ? (this.currentComponent = 'myComponent1')
              : (this.currentComponent = 'myComponent2')
          },
        },
      })
    </script>
  </body>
</html>
